<?php 

require("../include/config.php");
require("../API/php/fonctions.php");
require("../API/php/header.php");

// phpsql/mysql_getDateExpire.php?milis=123456789&lefttime=1254

//[x][0] = date_expiration

// IN obligatoire
$arrayGet = array(
    "lefttime" => null
);

$arrayValeur = recupGet($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
	// CONNECION SQL
	$db = mysql_connect($host, $base, $mdp)  or die('Erreur de connexion '.mysql_error());

	// CONNECION BASE
	mysql_select_db($base,$db)  or die('Erreur de selection '.mysql_error());
	
	$sql = "
	select `param_value` FROM `tab_parametres` where `param_name` = 'init_last_scan_tickets'
	";
	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
		$init_last_scan_tickets = $data['param_value']; 
	}
	
	$sql = "select MINUTE(timediff( date_format(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s'),'%H:%i:%s'), (SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s'),'%Y-%m-%d'))))
	+HOUR(timediff( date_format(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s'),'%H:%i:%s'), (SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s'),'%Y-%m-%d'))))*60 as nbMinuteLeftToday
	;";
	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
		$nbMinuteLeftToday = $data['nbMinuteLeftToday']; 
	}
	
	if($nbMinuteLeftToday >= $arrayValeur["lefttime"]){
		$sql = "select str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s') + INTERVAL ".$arrayValeur["lefttime"]." MINUTE as `date_expiration`
		;";
		
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

		// on fait une boucle qui va faire un tour pour chaque enregistrement 
		while($data = mysql_fetch_assoc($req)) 
		{ 
			$strSorti .=  $data['date_expiration']
			.'&'
			."\r\n"; 
		}
	}else{
		$reste = $arrayValeur["lefttime"] - $nbMinuteLeftToday;
		
		$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 1 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 1 DAY,'%Y-%m-%d'))))+
HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 1 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 1 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
		;";
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

		// on fait une boucle qui va faire un tour pour chaque enregistrement 
		while($data = mysql_fetch_assoc($req)) 
		{ 
			$nbMinuteNextDay = $data['nbMinuteNextDay']; 
		}
		
		if($nbMinuteNextDay >= $reste){
			$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 1 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 1 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
			;";
			
			$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

			// on fait une boucle qui va faire un tour pour chaque enregistrement 
			while($data = mysql_fetch_assoc($req)) 
			{ 
				$strSorti .=  $data['date_expiration']
				.'&'
				."\r\n"; 
			}
		}else{
			$reste = $reste - $nbMinuteNextDay;
			
			$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 2 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 2 DAY,'%Y-%m-%d'))))+
	HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 2 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 2 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
			;";
			$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

			// on fait une boucle qui va faire un tour pour chaque enregistrement 
			while($data = mysql_fetch_assoc($req)) 
			{ 
				$nbMinuteNextDay = $data['nbMinuteNextDay']; 
			}
			
			if($nbMinuteNextDay >= $reste){
				$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 2 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 2 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
				;";
				
				$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

				// on fait une boucle qui va faire un tour pour chaque enregistrement 
				while($data = mysql_fetch_assoc($req)) 
				{ 
					$strSorti .=  $data['date_expiration']
					.'&'
					."\r\n"; 
				}
			}else{
				$reste = $reste - $nbMinuteNextDay;
				
				$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 3 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 3 DAY,'%Y-%m-%d'))))+
		HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 3 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 3 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
				;";
				$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

				// on fait une boucle qui va faire un tour pour chaque enregistrement 
				while($data = mysql_fetch_assoc($req)) 
				{ 
					$nbMinuteNextDay = $data['nbMinuteNextDay']; 
				}
				
				if($nbMinuteNextDay >= $reste){
					$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 3 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 3 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
					;";
					
					$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

					// on fait une boucle qui va faire un tour pour chaque enregistrement 
					while($data = mysql_fetch_assoc($req)) 
					{ 
						$strSorti .=  $data['date_expiration']
						.'&'
						."\r\n"; 
					}
				}else{
					$reste = $reste - $nbMinuteNextDay;
					
					$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 4 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 4 DAY,'%Y-%m-%d'))))+
			HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 4 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 4 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
					;";
					$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

					// on fait une boucle qui va faire un tour pour chaque enregistrement 
					while($data = mysql_fetch_assoc($req)) 
					{ 
						$nbMinuteNextDay = $data['nbMinuteNextDay']; 
					}
					
					if($nbMinuteNextDay >= $reste){
						$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 4 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 4 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
						;";
						
						$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

						// on fait une boucle qui va faire un tour pour chaque enregistrement 
						while($data = mysql_fetch_assoc($req)) 
						{ 
							$strSorti .=  $data['date_expiration']
							.'&'
							."\r\n"; 
						}
					}else{
						$reste = $reste - $nbMinuteNextDay;
						
						$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 5 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 5 DAY,'%Y-%m-%d'))))+
				HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 5 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 5 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
						;";
						$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

						// on fait une boucle qui va faire un tour pour chaque enregistrement 
						while($data = mysql_fetch_assoc($req)) 
						{ 
							$nbMinuteNextDay = $data['nbMinuteNextDay']; 
						}
						
						if($nbMinuteNextDay >= $reste){
							$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 5 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 5 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
							;";
							
							$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

							// on fait une boucle qui va faire un tour pour chaque enregistrement 
							while($data = mysql_fetch_assoc($req)) 
							{ 
								$strSorti .=  $data['date_expiration']
								.'&'
								."\r\n"; 
							}
						}else{
							$reste = $reste - $nbMinuteNextDay;
							
							$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 6 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 6 DAY,'%Y-%m-%d'))))+
					HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 6 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 6 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
							;";
							$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

							// on fait une boucle qui va faire un tour pour chaque enregistrement 
							while($data = mysql_fetch_assoc($req)) 
							{ 
								$nbMinuteNextDay = $data['nbMinuteNextDay']; 
							}
							
							if($nbMinuteNextDay >= $reste){
								$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 6 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 6 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
								;";
								
								$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

								// on fait une boucle qui va faire un tour pour chaque enregistrement 
								while($data = mysql_fetch_assoc($req)) 
								{ 
									$strSorti .=  $data['date_expiration']
									.'&'
									."\r\n"; 
								}
							}else{
								$reste = $reste - $nbMinuteNextDay;
								
								$sql = "select MINUTE(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 7 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 7 DAY,'%Y-%m-%d'))))+
						HOUR(TIMEDIFF((SELECT  CONCAT(`close`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 7 DAY,'%Y-%m-%d')),(SELECT  CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 7 DAY,'%Y-%m-%d'))))*60 as  nbMinuteNextDay
								;";
								$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

								// on fait une boucle qui va faire un tour pour chaque enregistrement 
								while($data = mysql_fetch_assoc($req)) 
								{ 
									$nbMinuteNextDay = $data['nbMinuteNextDay']; 
								}
								
								if($nbMinuteNextDay >= $reste){
									$sql = "select STR_TO_DATE(CONCAT(STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 7 DAY,'%Y-%m-%d'), ' ',(SELECT CONCAT(`open`,':00') FROM `affectation` WHERE 1 and `date` = STR_TO_DATE(str_to_date('".$init_last_scan_tickets."','%Y/%m/%d %H:%i:%s')+ INTERVAL 7 DAY,'%Y-%m-%d'))), '%Y-%m-%d %H:%i:%s') + INTERVAL ".$reste." MINUTE as `date_expiration`
									;";
									
									$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

									// on fait une boucle qui va faire un tour pour chaque enregistrement 
									while($data = mysql_fetch_assoc($req)) 
									{ 
										$strSorti .=  $data['date_expiration']
										.'&'
										."\r\n"; 
									}
								}else{
									$strSorti .=  'WTF???:'.$reste;
								}
							}
						}
					}
				}
			}
		}
	}

	// DECONNECION SQL
	mysql_close();
}else{
	$strSorti = "ERROR:".$arrayValeur["error"];
}

require("../API/php/footer.php");
?>